Skip to main content

Q-3: Attempt the following (Any two) (10 Marks)

Questions

A: Differentiate between DVM and JVM.
B: State syntax to create Text View and Image button with any two attributes of each.
C: Describe Android service life cycle along with diagram.


Answer A: Differentiate between DVM and JVM

AspectDVM (Dalvik Virtual Machine)JVM (Java Virtual Machine)
Full FormDalvik Virtual MachineJava Virtual Machine
ArchitectureRegister-basedStack-based
File Format.dex (Dalvik Executable).class files
Memory UsageLess memory consumptionMore memory consumption
PerformanceFaster executionSlower compared to DVM
PlatformAndroid specificPlatform independent
OptimizationOptimized for mobile devicesGeneral purpose optimization
Multiple InstancesMultiple DVM instances can runSingle JVM instance per application
BytecodeDalvik bytecode (.dex)Java bytecode (.class)
Garbage CollectionConcurrent garbage collectorVarious GC algorithms
PurposeMobile application executionDesktop/server applications
Resource ConstraintsDesigned for limited resourcesNot specifically resource-constrained

Key Differences:

  1. DVM uses register-based architecture making it more efficient for mobile devices
  2. JVM uses stack-based architecture which is more traditional
  3. DVM runs .dex files while JVM runs .class files
  4. DVM is optimized for Android while JVM is platform-independent

Answer B: Syntax for TextView and ImageButton

TextView Syntax with Attributes

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="18sp"
android:textColor="#000000"
android:background="#FFFFFF"
android:padding="10dp"
android:gravity="center" />

Two Key Attributes for TextView:

  1. android:text - Sets the text content to display
  2. android:textSize - Sets the size of the text (in sp units)

ImageButton Syntax with Attributes

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_button"
android:background="@drawable/button_background"
android:contentDescription="Image Button"
android:scaleType="fitCenter"
android:padding="8dp"
android:onClick="imageButtonClick" />

Two Key Attributes for ImageButton:

  1. android:src - Sets the image/icon to display on the button
  2. android:background - Sets the background drawable or color for the button

Additional Common Attributes:

  • android:id - Unique identifier
  • android:layout_width/height - Dimensions
  • android:onClick - Click event handler
  • android:padding - Internal spacing

Answer C: Android Service Life Cycle with Diagram

Android Service Life Cycle

A service is a component that runs in the background to perform long-running operations without user interface.

Service Life Cycle Methods:

  1. onCreate() - Called when service is first created
  2. onStartCommand() - Called when service is started using startService()
  3. onBind() - Called when service is bound using bindService()
  4. onUnbind() - Called when all clients unbind from service
  5. onRebind() - Called when service is rebound after unbinding
  6. onDestroy() - Called when service is destroyed

Service Life Cycle Diagram:

    Service Created
|
onCreate()
|
┌─────────────────┐
│ │
▼ ▼
startService() bindService()
│ │
▼ ▼
onStartCommand() onBind()
│ │
▼ │
Service Running │
│ │
▼ ▼
stopService() unbindService()
│ │
▼ ▼
Service Stopped onUnbind()
│ │
└─────────┬───────┘

onDestroy()


Service Destroyed

Types of Services:

  1. Started Service (using startService())

    • Runs indefinitely in background
    • Must stop itself or be stopped by another component
  2. Bound Service (using bindService())

    • Provides client-server interface
    • Runs only while bound to components

Service States:

  • Created: Service object created but not running
  • Started: Service running after startService() call
  • Bound: Service bound to client components
  • Destroyed: Service stopped and resources released

Example Service Implementation:

public class MyService extends Service {

@Override
public void onCreate() {
super.onCreate();
// Initialize service
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Service logic here
return START_STICKY; // Restart if killed
}

@Override
public IBinder onBind(Intent intent) {
return null; // Return null for started service
}

@Override
public void onDestroy() {
super.onDestroy();
// Cleanup resources
}
}

← Back to Question Paper